package com.zhudeshuai.zhudemolist.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.zhudeshuai.zhudemolist.R;
import com.zhudeshuai.zhudemolist.adapter.PlayListAdapter;
import com.zhudeshuai.zhudemolist.base.BaseActivity;
import com.zhudeshuai.zhudemolist.util.AudioRecordUtil;
import com.zhudeshuai.zhudemolist.widget.PlayBox.AudioPlayView;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;

/**
 * 通用音频播放器
 *
 * @author zhudeshuai
 * @since 2018/7/11
 */
public class AudioPlayerDemoActivity extends BaseActivity {

	private RecyclerView mRecyclerView;

	private PlayListAdapter mAdapter;
	private AudioPlayView mAudioPlayView;
	private PopupWindow mPlayWindow;
	private boolean flag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_audio_player_demo);
		mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
		initRecyclerView();
		onRefresh(null);
	}

	private void initRecyclerView() {
		mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
		mAdapter = new PlayListAdapter(this);
		mAdapter.setItemClickListener(new PlayListAdapter.ItemClickListener() {
			@Override
			public void onItemClick(File file) {
				if(mAudioPlayView == null){
					mAudioPlayView = new AudioPlayView(AudioPlayerDemoActivity.this);
					mAudioPlayView.setOnCloseListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							mPlayWindow.dismiss();
						}
					});
				}
				mAudioPlayView.initSource(file.getAbsolutePath());
				if(mPlayWindow == null){
					mPlayWindow = new PopupWindow(mAudioPlayView, ViewGroup.LayoutParams.MATCH_PARENT,
							ViewGroup.LayoutParams.WRAP_CONTENT);
					mPlayWindow.setFocusable(false);
					mPlayWindow.setOutsideTouchable(false);
					mPlayWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
						@Override
						public void onDismiss() {
							mAudioPlayView.release();
						}
					});
				}
				mPlayWindow.showAtLocation(mRecyclerView, Gravity.START, 0, 0);
			}
		});

	}


	/**
	 * 遍历sd卡根目录下Music文件夹
	 *
	 * @param view
	 */
	public void onRefresh(View view) {
		File file = new File(Environment.getExternalStorageDirectory(), "Music");
		if (!file.exists()) {
			file.mkdir();
		}
		File[] files = file.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".m4a") || name.endsWith(".mp3");
			}
		});
		mAdapter.setData(Arrays.asList(files));
		mRecyclerView.setAdapter(mAdapter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mPlayWindow.dismiss();
	}


	/**
	 * 音频录制
	 * @param view
	 */
	public void onRecord(View view) {
		if(flag){
			stopRecord();
		}else {
			startRecord();
		}
		flag = !flag;
	}

	private void startRecord(){
		File fileDir = new File(Environment.getExternalStorageDirectory(), "Music");
		File file = new File(fileDir,System.currentTimeMillis() + ".m4a");
		AudioRecordUtil.start(file);
	}


	private void stopRecord(){
		AudioRecordUtil.stop();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		AudioRecordUtil.stop();
	}
}
